import { HCode } from "./code";
import moment from "moment";

export class HError extends Error {


  private code: HCode | number;

  constructor(code: HCode | number, message?: any) {
    super()
      super(message);
      this.code = code;
  }

  public toHttpResponseValue(oid?: string): HErrorResponse {
      return {
          st: moment().unix(),
          oid,
          code: this.code,
          message: this.message
      };
  }

  public static fromHttpResponseValue(value: HErrorResponse): HError {
      return new HError(value.code, value.message);
  }

  public toString(oid?: string): string {
      return JSON.stringify(this.toHttpResponseValue(oid), undefined, 2);
  }
}

export interface HErrorResponse {
  oid?: string;
  st?: number;
  code: HCode | number;
  message: string;
  url?: string;
  method?: string;
}